<template>
    <el-form :model="shop" label-position="right" label-width="auto">
        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="店名" prop="shopname">
                    <el-input v-model="shop.shopname" placeholder="店名" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="密码" prop="pwd">
                    <el-input v-model="shop.pwd" placeholder="密码" clearable/>
                </el-form-item>
            </el-col>
        </el-row>

        <el-row :gutter="10">

            <el-col :span="12">
                <el-form-item label="营业状态" prop="state">
                    <el-radio-group v-model="shop.state">
                        <el-radio value="1">营业中</el-radio>
                        <el-radio value="0">已打样</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-col>
        </el-row>

        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="手机号" prop="phone">
                    <el-input v-model="shop.phone" placeholder="手机号" clearable/>
                </el-form-item>
            </el-col>
        </el-row>

        <el-form-item label="地址" prop="address">
            <el-input v-model="shop.address" placeholder="地址" clearable/>
        </el-form-item>

        <el-form-item label="头像" prop="picture">
            <el-upload drag style="width:100%"
                       action="/api/file/upload"
                       :on-success="handleFileUploadSuccess"
            >
                <el-icon class="el-icon--upload"><upload-filled/></el-icon>
                <div class="el-upload__text">将文档拖拽到此处，或 <em>点击上传</em></div>
                <template #tip>
                    <div class="el-upload__tip">支持 jpg/png 格式， 大小不超过 5M</div>
                </template>
            </el-upload>
        </el-form-item>
    </el-form>

    <el-row>
        <el-col>
            <div style="text-align: center;">
                <el-button type="primary" @click="doSave">保存</el-button>
                <el-button @click="doCancel">取消</el-button>
            </div>
        </el-col>
    </el-row>
</template>

<script setup lang="ts">
import {ref, setDevtoolsHook} from "vue";
import {ElMessage} from "element-plus";
import axios from "axios";

//---------------------------- 自定义组件事件 -----------------------------
const emits = defineEmits(["closeDialog", "flushPage"]);

const shop = ref( {
    id: '',
    shopname: '',
    pwd: '',
    state: '1',
    phone: '',
    address: '',
    picture: ''
})
const handleFileUploadSuccess = (fileName) => {
    if (fileName != null || fileName != '') {
        shop.value.picture= fileName;
        ElMessage({type: 'success',message:'文件上传成功'})
    }else{
        ElMessage({type: 'error',message:'文件上传失败'})
    }
}
const doCancel = () => {
    emits("closeDialog");
}

// 新增
const doSave = async () => {
    console.log(shop.value);
    const {data} = await axios.post('/api/shop', shop.value);
    if (data > 0) {
        emits("flushPage");				// 触发事件： 刷新页面
        emits("closeDialog");			// 触发事件：关闭 dialog
        ElMessage.success("操作成功！");
    } else {
        ElMessage.error("操作失败！");
    }
}
</script>

<style scoped>

</style>